// 云函数入口文件
const cloud = require('wx-server-sdk')
const fs = require('fs')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const imgType = event.file.match(/^data:image\/(.*);base64/)
  const dealData = event.file.replace(/^data:image\/\w+;base64,/, '')
  const imageData = new Buffer.from(dealData, 'base64')
  const uploadData = await cloud.uploadFile({
    cloudPath: `./images/covers/${Date.now()}.${imgType[1]}`,
    fileContent: imageData
  })
  return uploadData
}
